﻿using BUS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWeb.GUI.User
{
    public partial class ChiTietSanPham : System.Web.UI.Page
    {
        int madt = 0;
        static int HangSX = 0;
        DienThoaiBUS dtBUS = new DienThoaiBUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            madt = int.Parse(Request["MaSP"].ToString());
            if (!Page.IsPostBack)
            {
                LoadCTDT();
                LoadHotProduct();
                LoadNewProduct();
                LoadSPCunghang();
                LoadThongSo();
            }
        }
        public void LoadCTDT()
        {
            DataTable dt = dtBUS.LayDuLieuChiTiet(madt);
            HangSX = Convert.ToInt32(dt.Rows[0]["MaHangSX"]);
            
            rpthongsokt.DataSource = dtBUS.LayDuLieuChiTiet(madt);
            rpthongsokt.DataBind();
            
        }
        public void LoadThongSo()
        {
            rpLoadCT.DataSource = dtBUS.LayDuLieuChiTiet(madt);
            rpLoadCT.DataBind();

        }
        public void LoadHotProduct()
        {
            rphotproduct.DataSource = dtBUS.LoadHotProduct();
            rphotproduct.DataBind();
        }
        public void LoadNewProduct()
        {
            rpNewProduct.DataSource = dtBUS.LoadNewProduct();
            rpNewProduct.DataBind();
        }

        public void LoadSPCunghang()
        {
            rpSPcunghang.DataSource = dtBUS.LoadSPCunghang(madt, HangSX);
            rpSPcunghang.DataBind();
        }

        protected void rpLoadCT_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Repeater rphinh = (Repeater)e.Item.FindControl("rpthumbnail");
            rphinh.DataSource = dtBUS.LayDuLieuHinhAnhSanPham(madt);
            rphinh.DataBind();
        }

        protected void rpLoadCT_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            TextBox txtsl = (TextBox)e.Item.FindControl("txtquantity");
            Label lbloi = (Label)e.Item.FindControl("lberror");
            if (e.CommandName.Equals("AddCart"))
            {
                int masp = Convert.ToInt32(e.CommandArgument);
                if (Convert.ToInt32(txtsl.Text) != 0)
                {
                    ShoppingCart.Instance.AddIteminDetail(masp, int.Parse(txtsl.Text));
                    Response.Redirect("GioHang.aspx");
                }
                
                
            }
        }

    }
}